package leetcode.editor.cn.q1_300.q100;

import leetcode.editor.cn.util.list.singly.ListNode;

/**
 * @author qidi
 * @date 2022-01-17 11:47:50
 */
class Q0141_LinkedListCycle {

    //leetcode submit region begin(Prohibit modification and deletion)
    public class Solution {
        public boolean hasCycle(ListNode head) {
            if (head == null || head.next == null || head.next.next == null) return false;
            // 快指针
            ListNode quickNode = head.next.next;
            // 慢指针
            ListNode slowNode = head;
            while (quickNode != null) {
                if (quickNode == slowNode) {
                    return true;
                }
                quickNode = quickNode.next;
                if (quickNode == null) return false;
                quickNode = quickNode.next;
                slowNode = slowNode.next;
            }
            return false;
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)


    public static void main(String[] args) {
        Solution solution = new Q0141_LinkedListCycle().new Solution();
    }
}